package com.egene.tw.activity.member;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.activity.member.liferecord.HealthListV2Activity_;
import com.egene.tw.activity.wiki.WikInfoActivity;
import com.egene.tw.activity.wiki.WikInfoActivity_;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.HealthItem;
import com.egene.tw.model.HealthList;
import com.egene.tw.model.report.LifeRecord;
import com.egene.tw.network.ApiConstant;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.egene.tw.ui.HealthView;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import java.util.List;

@EFragment(R.layout.fragment_health)
public class HealthFragment extends Fragment implements HealthView.OnItemClickListener {
    public final String TYPE_PERSONAL = "1";//type:1 个性化 2 营养食谱 3 运动养生
    public final String TYPE_FOOD_LIST = "2";
    public final String TYPE_SPORT = "3";
    public final String TYPE_PERSONAL_PROJECT = "4";
//    private final String TITLE_PERSONAL = "个性化";//type:1 个性化 2 营养食谱 3 运动养生
//    private final String TITLE_FOOD_LIST = "营养食谱";
//    private final String TITLE_SPORT = "运动养生";

    @ViewById
    LinearLayout ll_left_record;
    @ViewById
    TextView tvTitle_left_record;
    @ViewById
    TextView tvMore_left_record;

    @ViewById
    HealthView hvPersonalize;
    @ViewById
    HealthView hvFoodList;
    @ViewById
    HealthView hvSport;

    @Bean
    UserDao userDao;
    LayoutInflater mInflater;


    @AfterViews
    void initViews() {
        mInflater = LayoutInflater.from(getActivity());
        hvPersonalize.setOnItemClickListener(this);
        hvFoodList.setOnItemClickListener(this);
        hvSport.setOnItemClickListener(this);

        initMoreListener();
        fetchHealthList();
    }

    private void initMoreListener() {
        hvPersonalize.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = getString(R.string.personal_health_message);
                HealthListActivity_.intent(getActivity()).type("4").title(string).start();
            }
        });
        hvFoodList.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String string = getString(R.string.recommend_foods);
                HealthListActivity_.intent(getActivity()).type("1").title(string).start();
            }
        });
        hvSport.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String string = getString(R.string.recommend_sport);
                HealthListActivity_.intent(getActivity()).type("2").title(string).start();
            }
        });
    }

    void fetchHealthList() {
        //d424e0467e4b4e1684df9e42befdddaa
        EgeneApi.getHealthList(userDao.getUser().getUserID(), new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                HealthList result = JsonUtil.parse(responseBody, HealthList.class);
                if (result != null) {
                    if (result.isSuccess()) {
                        hvPersonalize.setData(R.drawable.personalise, getActivity().getString(R.string.personal_health_message), null);
                        hvFoodList.setData(R.drawable.food_list, getActivity().getString(R.string.recommend_foods), result.getPersonalitylist());
                        hvSport.setData(R.drawable.sport, getActivity().getString(R.string.recommend_sport), result.getNutritionlist());
                        updateLiftRecord(result.getLiferecorduserlist());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }
        });
    }


    private void updateLiftRecord(List<LifeRecord> lifeRecords) {
        if (lifeRecords == null || lifeRecords.size() == 0) {
            return;
        }
        int i = 0;
        for (final LifeRecord lifeRecord : lifeRecords) {
            if(i >= 7){
                break;
            }
            i++;
            View view = mInflater.inflate(R.layout.item_left_record, ll_left_record, false);
            TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            tvTitle.setText(lifeRecord.getLiftrecorddateformat());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String path = ApiConstant.BASE_URL + "/admin/ques/app_liferecord_view.aspx?userid=" + lifeRecord.getUserid() + "&liftrecorddate=" + lifeRecord.getLiftrecorddate();
                    PDFDetailActivity_.intent(getActivity()).title(lifeRecord.getLiftrecorddateformat()).path(path).start();
                }
            });
            ll_left_record.addView(view);
        }
        tvMore_left_record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HealthListV2Activity_.intent(getActivity()).title("個人生活記錄").start();
            }
        });
    }

    @Override
    public void onItemClick(HealthItem item) {
        Intent intent = new Intent(getActivity(), WikInfoActivity_.class);
        intent.putExtra(WikInfoActivity.ID, "" + item.getArticleid());
        startActivity(intent);
    }
}
